package cn.youframe.admin.dict;

import cn.youframe.frame.base.BaseCtrl;
import cn.youframe.frame.beans.AjaxVo;
import cn.youframe.frame.beans.CommTypeVO;
import cn.youframe.frame.beans.CommTypeValVO;
import cn.youframe.frame.exception.AppException;
import cn.youframe.frame.service.DictService;
import cn.youframe.frame.util.ToolsUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

/**
 * User: Administrator
 * DateTime: 13-2-19: 上午11:07
 */
@Controller
@RequestMapping(value = "/admin")
public class DictCtrl extends BaseCtrl{
    @Autowired
    private DictService dictService;

    /**
     * 查询所有类型
     * @param request
     * @return
     */
    @RequestMapping(value = "/DictCtrl!initTypes.do")
    public String initTypes(HttpServletRequest request){
        try{
            request.setAttribute("types",dictService.queryTypes());
        }catch (Exception ex){
            throw new AppException(ex,"获取类型异常！");
        }
        return "admin/dict/DictTypes";
    }

    /**
     * 类型值页面
     * @param request
     * @return
     */
    @RequestMapping(value = "/DictCtrl!typeValues.do")
    public String typeValues(HttpServletRequest request){
        String typeId=request.getParameter("typeId");
        String typeKind=request.getParameter("typeKind");
        String valMgrflag=request.getParameter("valMgrflag");
        List<CommTypeValVO> typeValList=new ArrayList<CommTypeValVO>();
        if(valMgrflag.equals("0")){//刷新模式
            Map<String, CommTypeValVO> tvMap=null;
            if(typeKind.equals("0")){//列表
                tvMap=ToolsUtil.getDictType(typeId);
                if(tvMap!=null){
                    Iterator<Map.Entry<String,CommTypeValVO>> tvit=tvMap.entrySet().iterator();
                    while (tvit.hasNext()){
                        typeValList.add(tvit.next().getValue());
                    }
                }
            }else {//树形

            }
        }else {//管理模式
            if(typeKind.equals("0")){//列表
                typeValList=dictService.getTypeVals(typeId,typeKind);
            }else {//树形

            }
        }
        request.setAttribute("typeVals",typeValList);
        return "admin/dict/DictValues";
    }
    /**
     * 添加类型
     * @param typeVO
     * @return
     */
    @RequestMapping(value = "/DictCtrl!addType.do")
    public @ResponseBody AjaxVo addType(CommTypeVO typeVO){
        AjaxVo ajaxVo=new AjaxVo();
        try{
            dictService.addType(typeVO);
            ajaxVo.setMessage("添加成功！");
        }catch (Exception ex){
            ajaxVo.setFlag(false);
            ajaxVo.setMessage("添加失败："+ex.getMessage());
            logger.error("添加失败："+ex.getMessage(),ex);
        }
        return ajaxVo;
    }

    /**
     * 修改类型
     * @param parasMap
     * @return
     */
    @RequestMapping(value = "/DictCtrl!updateType.do")
    public @ResponseBody AjaxVo updateType(@RequestParam Map<String,String> parasMap){
        AjaxVo ajaxVo=new AjaxVo();
        try{
            dictService.doUpdateType(parasMap);
            ajaxVo.setMessage("修改成功！");
        }catch (Exception ex){
            ajaxVo.setFlag(false);
            ajaxVo.setMessage("修改失败："+ex.getMessage());
            logger.error("修改失败："+ex.getMessage(),ex);
        }
        return ajaxVo;
    }

    /**
     * 删除类型
     * @param typeId
     * @return
     */
    @RequestMapping(value = "/DictCtrl!delType.do")
    public @ResponseBody AjaxVo delType(@RequestParam String typeId){
        AjaxVo ajaxVo=new AjaxVo();
        try{
            dictService.doDelType(typeId);
            ajaxVo.setMessage("删除成功！");
        }catch (Exception ex){
            ajaxVo.setFlag(false);
            ajaxVo.setMessage("删除失败："+ex.getMessage());
            logger.error("删除失败："+ex.getMessage(),ex);
        }
        return ajaxVo;
    }
}
